﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Port : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F2561;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPortID;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblPort;
        protected Label lblPortID;
        protected Label lblPortName;
        protected Label lblRemark;
        protected HtmlLink mycss;
        protected Label one1;
        protected TextBox txtPortID;
        protected TextBox txtPortName;
        protected TextBox txtRemark;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存不能复制!";
            }
            else if (this.hdnCopy.Value != "0")
            {
                this.lblMsg.Text = "已经是复制状态不能重复复制!";
            }
            else
            {
                this.txtPortID.Enabled = true;
                this.txtPortID.Text = "";
                this.hdnCopy.Value = "1";
                this.hdnFlag.Value = "0";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存不能删除!";
            }
            else
            {
                try
                {
                    List<string> PortID = new List<string> {
                        this.txtPortID.Text.Trim()
                    };
                    this.lblMsg.Text = "删除港口 " + BaseInfoServiceClientProxy.GetInstance().Delete(PortID, "PORT") + "成功!";
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("PortList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Exception ex;
            try
            {
                if (string.IsNullOrEmpty(this.txtPortName.Text))
                {
                    this.lblMsg.Text = "港口名称不能为空!";
                }
                else
                {
                    PortInfo portInfo = null;
                    if (base.DataEntityViewState != null)
                    {
                        portInfo = base.DataEntityViewState as PortInfo;
                    }
                    else
                    {
                        portInfo = new PortInfo();
                    }
                    portInfo.PortName = this.txtPortName.Text.Trim();
                    portInfo.Remark = this.txtRemark.Text.Trim();
                    portInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    if (this.hdnFlag.Value == "1")
                    {
                        portInfo.PortID = this.txtPortID.Text.Trim();
                        portInfo.UpdatedBy = base.UserInfo.UserID;
                        this.lblMsg.Text = "更新" + BaseInfoServiceClientProxy.GetInstance().Update(portInfo, "PORT") + "成功";
                    }
                    else
                    {
                        try
                        {
                            portInfo.PortID = this.txtPortID.Text.Trim();
                            portInfo.CreatedBy = base.UserInfo.UserID;
                            this.txtPortID.Text = BaseInfoServiceClientProxy.GetInstance().Insert(portInfo, "PORT");
                        }
                        catch (Exception exception1)
                        {
                            ex = exception1;
                            this.lblMsg.Text = "新增失败,港口：" + this.txtPortID.Text.Trim() + "已存在";
                            return;
                        }
                        this.lblMsg.Text = "新增" + this.txtPortID.Text + "成功";
                        base.DataEntityViewState = portInfo;
                        this.hdnFlag.Value = "1";
                        this.hdnCopy.Value = "0";
                    }
                    this.txtPortID.Enabled = false;
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnPortID.Value = base.Request.QueryString["ID"];
                this.hdnFlag.Value = "1";
                this.SetPortInfo2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPortInfo2Form()
        {
            string PortID = this.hdnPortID.Value;
            PortInfo portInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(PortID, "PORT") as PortInfo;
            base.DataEntityViewState = portInfo;
            if (portInfo != null)
            {
                this.txtPortID.Text = portInfo.PortID;
                this.txtPortName.Text = portInfo.PortName;
                this.txtRemark.Text = portInfo.Remark;
                this.chkIsActive.Checked = portInfo.IsActive == "Y";
            }
            this.txtPortID.Enabled = false;
        }
    }
}

